package org.zjx.util.clickprecision;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.List;

/**
 * 游戏点击管理器 - 整合坐标转换和碰撞检测
 * 提供简洁易用的API接口
 */
public class GameClickManager {
    private final PixelCoordinateSystem coordinateSystem;
    private final PixelCollisionSystem collisionSystem;
    private List<Rectangle> blockedAreas;

    public GameClickManager(int screenWidth, int screenHeight, int pixelsPerUnit,
                            int gameMapWidth, int gameMapHeight, List<Rectangle> blockedAreas) {
        this.coordinateSystem = new PixelCoordinateSystem(screenWidth, screenHeight, pixelsPerUnit,
                gameMapWidth, gameMapHeight);
        this.collisionSystem = new PixelCollisionSystem(screenWidth, screenHeight);
        this.blockedAreas = blockedAreas;
    }

    /**
     * 计算最优点击位置 - 主要API
     */
    public Point calculateOptimalClick(Point playerGamePos, Point targetGamePos) {
        // 1. 计算目标屏幕位置
        Point targetScreenPos = coordinateSystem.calculateScreenClickPosition(playerGamePos, targetGamePos);

        // 2. 获取屏幕中心（起点）
        Point screenCenter = coordinateSystem.getScreenCenter();

        // 3. 寻找最优点击位置
        return collisionSystem.findOptimalClickPosition(screenCenter, targetScreenPos, blockedAreas);
    }

    public Point calculateOptimalClickByCenterPos(Point screenCenterPos, Point targetGamePos) {
        // 1. 计算目标屏幕位置
        Point targetScreenPos = coordinateSystem.calculateScreenClickPositionByCenterPos(screenCenterPos, targetGamePos);

        // 2. 获取屏幕中心（起点）
        Point screenCenter = coordinateSystem.getScreenCenter();

        // 3. 寻找最优点击位置
        return collisionSystem.findOptimalClickPosition(screenCenter, targetScreenPos, blockedAreas);
    }


    /**
     * 快速检查是否可以直击目标
     */
    public boolean canClickDirectly(Point playerGamePos, Point targetGamePos) {
        Point screenPos = coordinateSystem.calculateScreenClickPosition(playerGamePos, targetGamePos);
        return collisionSystem.isPositionClickable(screenPos, blockedAreas);
    }

    /**
     * 批量计算点击位置
     */
    public Point[] calculateMultipleClicks(Point playerGamePos, Point[] targetGamePositions) {
        Point screenCenter = coordinateSystem.getScreenCenter();
        Point[] targetScreens = new Point[targetGamePositions.length];

        for (int i = 0; i < targetGamePositions.length; i++) {
            targetScreens[i] = coordinateSystem.calculateScreenClickPosition(playerGamePos, targetGamePositions[i]);
        }

        return collisionSystem.findMultipleOptimalPositions(screenCenter, targetScreens, blockedAreas);
    }

    /**
     * 获取目标点相对于屏幕中心的偏移向量
     */
    public Point getTargetOffsetVector(Point playerGamePos, Point targetGamePos) {
        Point targetPixel = coordinateSystem.gameToPixelMap(targetGamePos);
        Point screenCenterPixel = coordinateSystem.calculateScreenCenterInPixelMap(playerGamePos);

        return new Point(
                targetPixel.x - screenCenterPixel.x,
                -(targetPixel.y - screenCenterPixel.y)
        );
    }

    /**
     * 更新阻挡区域
     */
    public void updateBlockedAreas(List<Rectangle> newBlockedAreas) {
        this.blockedAreas = newBlockedAreas;
    }

    /**
     * 获取屏幕中心
     */
    public Point getScreenCenter() {
        return coordinateSystem.getScreenCenter();
    }
}